package com.xmtlxx.system.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.IdUtil;
import com.xmtlxx.common.core.domain.PageQuery;
import com.xmtlxx.common.core.domain.entity.SysOperator;
import com.xmtlxx.common.core.domain.entity.SysWorklog;
import com.xmtlxx.common.core.page.TableDataInfo;
import com.xmtlxx.common.enums.UserType;
import com.xmtlxx.common.helper.LoginHelper;
import com.xmtlxx.common.utils.StringUtils;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.xmtlxx.system.domain.bo.SysWorklogBo;
import com.xmtlxx.system.domain.vo.SysOperatorVo;
import com.xmtlxx.system.domain.vo.SysWorklogVo;
import com.xmtlxx.system.mapper.SysOperatorMapper;
import com.xmtlxx.system.mapper.SysWorklogMapper;
import com.xmtlxx.system.service.ISysWorklogService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.util.Collection;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 流程日志Service业务层处理
 *
 * @author xmtlxx
 * @date 2022-04-24
 */
@Slf4j
@RequiredArgsConstructor
@Service
public class SysWorklogServiceImpl implements ISysWorklogService {

    private final SysWorklogMapper baseMapper;

    private final SysOperatorMapper operatorMapper;

    /**
     * 查询流程日志
     *
     * @param logid 流程日志主键
     * @return 流程日志
     */
    @Override
    public SysWorklogVo queryById(String logid){
        return baseMapper.selectVoById(logid);
    }

    /**
     * 查询流程日志列表
     *
     * @param bo 流程日志
     * @return 流程日志
     */
    @Override
    public TableDataInfo<SysWorklogVo> queryPageList(SysWorklogBo bo, PageQuery pageQuery) {
//        LambdaQueryWrapper<SysWorklog> lqw = buildQueryWrapper(bo);
        Page<SysWorklogVo> result = baseMapper.selectPageSysWorklogList(pageQuery.build(), bo);
        return TableDataInfo.build(result);
    }

    /**
     * 查询流程日志列表
     *
     * @param bo 流程日志
     * @return 流程日志
     */
    @Override
    public List<SysWorklogVo> queryList(SysWorklogBo bo) {
//        LambdaQueryWrapper<SysWorklog> lqw = buildQueryWrapper(bo);
        return baseMapper.selectSysWorklogList(bo);
    }

    private LambdaQueryWrapper<SysWorklog> buildQueryWrapper(SysWorklogBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<SysWorklog> lqw = Wrappers.lambdaQuery();
        lqw.eq(StringUtils.isNotBlank(bo.getWid()), SysWorklog::getWid, bo.getWid());
        lqw.eq(StringUtils.isNotBlank(bo.getNid()), SysWorklog::getNid, bo.getNid());
        lqw.eq(StringUtils.isNotBlank(bo.getObjid()), SysWorklog::getObjid, bo.getObjid());
        lqw.like(StringUtils.isNotBlank(bo.getName()), SysWorklog::getName, bo.getName());
        lqw.eq(StringUtils.isNotBlank(bo.getBusstype()), SysWorklog::getBusstype, bo.getBusstype());
        lqw.eq(bo.getOperatorid() != null, SysWorklog::getOperatorid, bo.getOperatorid());
        lqw.eq(StringUtils.isNotBlank(bo.getOtype()), SysWorklog::getOtype, bo.getOtype());
        lqw.like(StringUtils.isNotBlank(bo.getOname()), SysWorklog::getOname, bo.getOname());
        lqw.eq(bo.getUserId() != null, SysWorklog::getUserId, bo.getUserId());
        lqw.eq(bo.getDeptId() != null, SysWorklog::getDeptId, bo.getDeptId());
        lqw.eq(StringUtils.isNotBlank(bo.getStatus()), SysWorklog::getStatus, bo.getStatus());
        lqw.eq(StringUtils.isNotBlank(bo.getPrelogid()), SysWorklog::getPrelogid, bo.getPrelogid());
        lqw.eq(bo.getPreid() != null, SysWorklog::getPreid, bo.getPreid());
        lqw.eq(StringUtils.isNotBlank(bo.getPrenid()), SysWorklog::getPrenid, bo.getPrenid());
        lqw.eq(StringUtils.isNotBlank(bo.getAction()), SysWorklog::getAction, bo.getAction());
        lqw.eq(StringUtils.isNotBlank(bo.getPid()), SysWorklog::getPid, bo.getPid());
        lqw.orderByAsc(SysWorklog::getCreateTime);
        return lqw;
    }

    /**
     * 新增流程日志
     *
     * @param bo 流程日志
     * @return 结果
     */
    @Override
    public Boolean insertByBo(SysWorklogBo bo) {
        SysWorklog add = BeanUtil.toBean(bo, SysWorklog.class);
        validEntityBeforeSave(add);

        //填充UUID
        add.setLogid(IdUtil.simpleUUID());

        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setLogid(add.getLogid());
        }
        return flag;
    }

    /**
     * 修改流程日志
     *
     * @param bo 流程日志
     * @return 结果
     */
    @Override
    public Boolean updateByBo(SysWorklogBo bo) {
        SysWorklog update = BeanUtil.toBean(bo, SysWorklog.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 保存前的数据校验
     *
     * @param entity 实体类数据
     */
    private void validEntityBeforeSave(SysWorklog entity){
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 批量删除流程日志
     *
     * @param ids 需要删除的流程日志主键
     * @return 结果
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<String> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteBatchIds(ids) > 0;
    }

    /**
     * 获取待办信息
     *
     * @param bo
     * @return
     */
    @Override
    public SysWorklogVo getNeedWorklog(SysWorklogBo bo) {
        SysWorklogVo vo = baseMapper.getNeedWorklog(bo);
        if(vo != null){
            LambdaQueryWrapper<SysOperator> lqw = Wrappers.lambdaQuery();
            lqw.eq(SysOperator::getNid,vo.getNid());
            lqw.eq(SysOperator::getType,"1");
            List<SysOperatorVo> operators = operatorMapper.selectVoList(lqw);
            vo.setOperators(operators);
        }
        return vo;
    }

    /**
     * 获取经办人
     * @param bo
     * @return
     */
    @Override
    public List<SysWorklogVo> queryWorkOperatores(SysWorklogBo bo) {
        return baseMapper.queryWorkOperatores(bo);
    }

    @Override
    public Map<String, Object> selectDayCounts(String beginDay, String endDay, String status,String objid) {
        SysWorklogBo bo = new SysWorklogBo();
        if(LoginHelper.getUserType().equals(UserType.SYS_USER)){
            bo.setUserId(LoginHelper.getUserId());
            bo.setDeptId(LoginHelper.getDeptId());
        }else if(LoginHelper.getUserType().equals(UserType.PATROL)){
            bo.setUserId(LoginHelper.getUserId());
        }
        bo.setStatus(status);
        bo.setOid(objid);
        Map<String,Object> params = new HashMap<>();
        params.put("beginDotime",beginDay);
        params.put("endDotime",endDay);
        bo.setParams(params);
        return baseMapper.selectDayCounts(bo);
    }
}
